<link rel="stylesheet" href="../static/layui/lib/jq-module/zyupload/zyupload-1.0.0.min.css" media="all">

<style>
    .layui-table-col-special .layui-table-cell {
        height: auto;
        overflow: visible;
        text-overflow: inherit;
    }


</style>
<div class="layuimini-container layuimini-page-anim">
    <script type="text/html" id="toolbarDemo">
        <div class="layui-btn-container" id="buttons">
            <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加库存</button>
            <button class="layui-btn layui-btn-danger layui-btn-sm data-add-btn" lay-event="get_price"> 更新价格(半小时内请勿重复点击)</button>
            <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add_cookie"> 设置Cookie</button>
            <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add_proxy"> 设置代理</button>
            <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add_skin"> 新饰品搜不到？</button>
        </div>
    </script>
    <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
    <script type="text/html" id="currentTableBar">
        <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="add_rent">增加一笔租金</a>
        <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="delete_rent">删除一笔租金</a>
        <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete_all">删除</a>
    </script>
</div>

<script src="../static/layui/lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="../static/layui/lib/jq-module/zyupload/zyupload-1.0.0.min.js" charset="utf-8"></script>
<script src="../static/layui/js/get_sessionstorage.js"></script>
<script>
    async_set_stock_cache()
    layui.use(['table'], function () {
        var $ = layui.jquery,
            table = layui.table;

        table.render({
            elem: '#currentTableId',
            url: '/api/csgostock?type=2',
            toolbar: '#toolbarDemo',
            defaultToolbar: [],
            cols: [[
                {field: 'stock_id', width: 75, title: 'ID', align: "center"},
                {field: 'item_id', width: 150, title: '饰品ID',sort: true, align: "center"},
                {field: 'item_name', width: 300, title: '饰品名称',sort: true, align: "center"},
                {field: 'buy_price', width: 150, title: '购入价', sort: true, align: "center"},
                {field: 'current_price', width: 150, title: '当前价', sort: true, align: "center"},
                {title: '操作', minWidth: 250, toolbar: '#currentTableBar', align: "center"},
                {field: 'price_mtime', width: 200, title: '最新价格时间', sort: true, align: "center"},
                {field: 'rent_interest', width: 200, title: '租金收益率(%)', sort: true, align: "center"},
                {field: 'rent_interest_all', width: 200, title: '总收益率(%)', sort: true, align: "center"},
                {field: 'sum_rent', width: 250, title: '总租金', sort: true, align: "center"},
                {field: 'split_rent', width: 250, title: '租金', align: "center"},
                {field: 'price_diff_from_last_date', width: 100, title: '较昨日', sort: true, align: "center"}
            ]],
            limits: [50, 100, 200, 500],
            limit: 100,
            page: true,
            skin: 'line'
        });


        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加新库存',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['50%', '50%'],
                    content: '../static/layui/page/menu_child/csgo_stock/add_stock_list.html',
                    end: function () {
                        layui.miniAdmin.success('添加成功');
                        layui.table.reload('currentTableId');
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
            if (obj.event === 'add_skin') {  // 监听添加操作
                var index = layer.open({
                    title: '添加新饰品',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['50%', '50%'],
                    content: '../static/layui/page/menu_child/csgo_stock/add_skin.html',
                    end: function () {
                        // layui.table.reload('currentTableId');
                        layui.miniAdmin.success('添加成功');
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
            if (obj.event === 'add_cookie') {  // 监听添加操作
                var index = layer.open({
                    title: '修改cookie',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['50%', '50%'],
                    content: '../static/layui/page/menu_child/csgo_stock/add_cookie_list.html',
                    end: function () {
                        layui.miniAdmin.success('添加成功');
                        // layui.table.reload('currentTableId');
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
            if (obj.event === 'add_proxy') {  // 监听添加操作
                var index = layer.open({
                    title: '设置proxy',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['50%', '50%'],
                    content: '../static/layui/page/menu_child/csgo_stock/add_proxy.html',
                    end: function () {
                        layui.miniAdmin.success('添加成功');
                        // layui.table.reload('currentTableId');
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
            if (obj.event === 'get_price') {  // 监听获取操作
            var formObject = {};
            formObject["type"]=6;
            var data_obj = JSON.stringify(formObject);
            $.ajax({
                type : 'POST',
                url : "/api/csgostock",
                data : data_obj,
                dataType: "json",
                contentType:'application/json',
                async: true,
                success : function(result){
                    if (result.success == "false") {
                        alert("更新出错!");
                    }
                    else if (result.exception == "true"){
                        alert("服务器异常");
                    }
                    else if (result.success == "true"){
                        alert("更新中(约5分钟)...");
                    }
			    },error : function(result){
			            console.log(result);
                    }
		    });
            }
        });
        table.on('tool(currentTableFilter)', function (obj) {
            <!--监听 add_rent 按钮 操作-->
            if (obj.event === 'add_rent') {  // 监听添加操作
                var index = $(obj.tr).attr("data-index");
                data = layui.table.cache["currentTableId"];
                var stock_id = data[index]["stock_id"];
                var item_id = data[index]["item_id"];
                layer.open({
                    title: '增加租金',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['35%', '75%'],
                    content: "../static/layui/page/menu_child/csgo_stock/add_rent.html?item_id=" + item_id + "&stock_id=" + stock_id,
                    end: function () {
                        layui.table.reload('currentTableId');
                    }
                });
            }
            <!--监听 delete_rent 按钮 操作-->
            if (obj.event == 'delete_rent') {
                layer.confirm('确认删除租金？(仅最后一笔)', {
                    title: '确认',
                    btn: ['确认', '取消']
                }, function () {
                    var formObject = {};
                    var index = $(obj.tr).attr("data-index");
                    data = layui.table.cache["currentTableId"];
                    var stock_id = data[index]["stock_id"];
                    formObject["type"] = 2;
                    formObject["stock_id"] = stock_id;
                    var data_obj = JSON.stringify(formObject);
                    $.ajax({
                        type: 'POST',
                        url: "/api/csgostockrent",
                        data: data_obj,
                        dataType: "json",
                        contentType: 'application/json',
                        async: false,
                        success: function (result) {
                            if (result.success == "false") {
                                alert("删除失败!");
                                layer.closeAll('dialog');
                            } else if (result.exception == "true") {
                                alert("服务器异常");
                                layer.closeAll('dialog');
                            } else if (result.success == "true") {
                                // 关闭弹出层
                                layer.msg("已删除数据！");
                                layui.table.reload('currentTableId');
                            }
                        }, error: function (result) {
                            console.log(result);
                        }
                    });
                });
            }
            <!--监听 delete_all 按钮 操作-->
            if (obj.event == 'delete_all') {
                layer.confirm('确认删除租金？(仅最后一笔)', {
                    title: '确认',
                    btn: ['确认', '取消']
                }, function () {
                    var formObject = {};
                    var index = $(obj.tr).attr("data-index");
                    data = layui.table.cache["currentTableId"];
                    var stock_id = data[index]["stock_id"];
                    formObject["type"] = 3;
                    formObject["stock_id"] = stock_id;
                    var data_obj = JSON.stringify(formObject);
                    $.ajax({
                        type: 'POST',
                        url: "/api/csgostockrent",
                        data: data_obj,
                        dataType: "json",
                        contentType: 'application/json',
                        async: false,
                        success: function (result) {
                            if (result.success == "false") {
                                alert("删除失败!");
                                layer.closeAll('dialog');
                            } else if (result.exception == "true") {
                                alert("服务器异常");
                                layer.closeAll('dialog');
                            } else if (result.success == "true") {
                                // 关闭弹出层
                                layer.msg("已删除数据！");
                                layui.table.reload('currentTableId');
                            }
                        }, error: function (result) {
                            console.log(result);
                        }
                    });
                });
            }
        });
    });

</script>